package com.pig4cloud.pig.djjk.service.wd;

import com.pig4cloud.pig.djjk.constant.MoveDirection;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @className: DirJudgeService
 * @description: TODO 类描述
 * @author: liumingkun
 * @date: 2025/6/25
 **/
@Service
public class DirJudgeService {


	/**
	 * 判断矩形是否在区域内
	 *
	 * @param region 区域坐标 [x1, y1, x2, y2]
	 * @param rect   矩形坐标 [x1, y1, x2, y2]
	 * @return 是否在区域内
	 */
	public boolean isInsideRegion(List<Integer> region, List<Integer> rect) {
		// 矩形中心点
		double rectCenterX = (rect.get(0) + rect.get(2)) / 2.0;
		double rectCenterY = (rect.get(1) + rect.get(3)) / 2.0;

		// 检查中心点是否在区域内
		return rectCenterX >= region.get(0) &&
				rectCenterX <= region.get(2) &&
				rectCenterY >= region.get(1) &&
				rectCenterY <= region.get(3);
	}


	public  MoveDirection analyzeMovementByCenter(List<Integer> region, List<Integer> lastPos, List<Integer> firstPos) {
		boolean wasInsideFirst = isInsideRegionByCenter(region, firstPos);
		boolean wasInsideLast = isInsideRegionByCenter(region, lastPos);

		if (!wasInsideFirst && wasInsideLast) {
			return MoveDirection.ENTER;
		} else if (wasInsideFirst && !wasInsideLast) {
			return MoveDirection.EXIT;
		} else if (wasInsideFirst && wasInsideLast) {
			return MoveDirection.REMAIN_INSIDE;
		} else {
			return MoveDirection.REMAIN_OUTSIDE;
		}
	}

	private  boolean isInsideRegionByCenter(List<Integer> region, List<Integer> rect) {
		// 矩形中心点
		double rectCenterX = rect.get(0) / 1.0;
		double rectCenterY = rect.get(1) / 1.0;

		// 检查中心点是否在区域内
		return rectCenterX >= region.get(0) &&
				rectCenterX <= region.get(2) &&
				rectCenterY >= region.get(1) &&
				rectCenterY <= region.get(3);
	}
}
